﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace KeyRemapper
{
    public class Hotkeys
    {
        private KeyboardHook _hook;

        public List<Hotkey> HotkeyList { get; set; }
        public bool Enabled { get; set; }

        public Hotkeys(KeyboardHook hook)
        {
            _hook = hook;

            _hook.OnKeyPressed += _hook_OnKeyPressed;
        }
        ~Hotkeys()
        {
            _hook.OnKeyPressed -= _hook_OnKeyPressed;
        }

        private void _hook_OnKeyPressed(Keys k, ref bool block)
        {
            if (block)
                return;

            if (Enabled && HotkeyList != null && HotkeyList.Count > 0)
            {
                try
                {
                    for (int i = 0; i < HotkeyList.Count; i++)
                    {
                        if (HotkeyList[i].Key == k &&
                            HotkeyList[i].Alt == ((Control.ModifierKeys & Keys.Alt) == Keys.Alt) &&
                            HotkeyList[i].Shift == ((Control.ModifierKeys & Keys.Shift) == Keys.Shift) &&
                            HotkeyList[i].Ctrl == ((Control.ModifierKeys & Keys.Control) == Keys.Control))
                        {
                            OnHotkeyPressed(HotkeyList[i]);

                            block = true;
                            break;
                        }
                    }
                }
                catch { }
            }
        }

        public event Action<Hotkey> OnHotkeyPressed = null;
    }
    public class Hotkey
    {
        public Keys Key { get; set; }

        public bool Shift { get; set; }
        public bool Ctrl { get; set; }
        public bool Alt { get; set; }

        public Hotkey()
        {
        }
    }
}
